package com.example.conference.service;

import com.example.conference.domain.CourseStudent;
import com.example.conference.domain.CourseTeacher;
import com.example.conference.domain.dto.PageParam;
import com.example.conference.domain.vo.*;
import com.example.conference.mapper.CourseStudentMapper;
import com.example.conference.mapper.CourseTeacherMapper;
import com.example.conference.utils.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseStudentServiceImpl {
    @Autowired
    CourseStudentMapper courseStudentMapper;
    @Autowired
    CourseTeacherMapper courseTeacherMapper;

    public Result addCourseStudent(CourseStudent courseStudent) {
        if (courseStudentMapper.check(courseStudent) == null) {
            if (courseStudentMapper.add(courseStudent) != 0) {
                CourseTeacher cT = courseTeacherMapper.queryById(courseStudent.getCtId());
                if (cT.getCurNum() < cT.getMaxNum()) {
                    courseTeacherMapper.updateCurNum(cT.getCtId(), cT.getCurNum()+1);
                } else {
                    return Result.error("选课人数超出限制");
                }
                return Result.success().data("items", "添加成功");
            } else {
                return Result.error("添加错误");
            }
        } else {
            return Result.error("课程已经选过");
        }
    }

    public Result update(CourseStudent courseStudent) {
        Integer tmp = courseStudentMapper.update(courseStudent);
        if (tmp != null) {
            if(tmp != 0) {
                return Result.success().data("items","成绩录入成功!");
            } else {
                return Result.error("成绩录入失败!");
            }
        }else {
            return Result.error("数据不存在，请联系管理员!");
        }
    }

    public Result delete(Integer csId) {
        CourseTeacher cT = courseTeacherMapper.queryById(courseStudentMapper.queryctIdBycsId(csId));
        Integer tmp = courseStudentMapper.delete(csId);
        if (tmp != null && tmp != 0 )
        {
            courseTeacherMapper.updateCurNum(cT.getCtId(), cT.getCurNum()-1);
            return Result.success().data("items", "删除成功");
        } else {
            return Result.error("删除失败");
        }

    }

    public Result querySelectedCourse(PageParam page,Integer userId) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        Page<CourseStudentInfoVo> courseStudents = courseStudentMapper.queryByStudentToList(userId);
        return Result.success().data("items",courseStudents.doSelectPageInfo(()-> courseStudentMapper.queryByStudentToList(userId)));
    }

    public Result queryCourseTeahcerByMajor(PageParam page,Integer userId, Integer majorId) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        Page<CourseTeacherForStudentVo> courseTeacherVos = courseTeacherMapper.queryByMajor(userId,majorId);
        return Result.success().data("items",courseTeacherVos.doSelectPageInfo(()-> courseTeacherMapper.queryByMajor(userId,majorId)));
    }

    public Result queryStudentInfo(PageParam page, Integer ctId) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        Page<CourseStudentInfo> studentInfos = courseStudentMapper.queryByctIdToStudentInfo(ctId);
        return Result.success().data("items",studentInfos.doSelectPageInfo(()-> courseStudentMapper.queryByctId(ctId)));
    }
}
